<!--
* @Description: 精英pk赛/学校奖励派发 - 查看明细
* @Author: liyujie
* @Date: 2021/11/09
* @Last Modified by: liyujie
* @Last Modified time: 2021/011/09
-->
<template>
    <PageLayout :title="`所在学校：${formData.school_keyword}`">
        <template slot="header-right">
            <div
                class="back"
                @click="handleRoute('elitePkStatistics', {
                    active: 'school-award'
                })"
            >
                返回
            </div>
        </template>
        <template slot="filter">
            <div class="filter-item">
                <div class="filter-item__title">
                    用户信息
                </div>
                <el-input
                    v-model="formData.keyword"
                    placeholder="请输入用户编号/手机号"
                    size="small"
                    clearable
                    suffix-icon="el-icon-search"
                    @change="getTableData(1)"
                    @keyup.native.enter="getTableData(1)"
                />
            </div>
            <div class="filter-item">
                <div class="filter-item__title">
                    活动资格
                </div>
                <el-select
                    v-model="formData.cancel_status"
                    clearable
                    filterable
                    placeholder="请选择"
                    size="small"
                    @change="getTableData(1)"
                >
                    <el-option
                        v-for="item in statusList"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value"
                    />
                </el-select>
            </div>
        </template>
        <el-table
            :data="tableData"
            height="100%"
            style="width: 100%;"
            ref="zsdxEliteSend"
            class="data-table"
            @selection-change="handleSelectedChange"
            @sort-change="handleSortChanged"
        >
            <el-table-column
                type="selection"
                width="60"
                align="center"
            />
            <el-table-column
                type="index"
                width="60"
                label="序号"
                align="center"
            />
            <el-table-column
                prop="index"
                label="活动期数"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
            >
                <template slot-scope="scope">
                    第{{ scope.row.index ? scope.row.index : '0' }}期
                </template>
            </el-table-column>
            <el-table-column
                prop="true_name"
                label="用户信息"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="left"
                :width="180"
            >
                <template slot-scope="scope">
                    <div class="__flex_wrap">
                        <p class="__flex_wrap_title">{{ scope.row.true_name }}</p>
                        <p>手机号:{{ scope.row.phone }}</p>
                        <p>精英等级:E{{ scope.row.agent_level }}</p>
                    </div>
                </template>
            </el-table-column>
            <el-table-column
                prop="school_name"
                label="学校名称"
            />
            <el-table-column
                prop="team_order_money"
                label="团队实付金额"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="left"
                sortable="custom"
                :width="140"
            >
                <template slot-scope="scope">
                    {{ scope.row.team_order_money ? scope.row.team_order_money : 0 }}
                </template>
            </el-table-column>
            <el-table-column
                prop="team_real_order_count"
                label="最终有效订单数"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="left"
                sortable="custom"
                :width="150"
            >
                <template slot-scope="scope">
                    {{ scope.row.team_real_order_count ? scope.row.team_real_order_count : 0 }}
                </template>
            </el-table-column>
            <el-table-column
                prop="rank"
                label="排名"
                show-overflow-tooltip
                align="left"
                sortable="custom"
            >
                <template slot-scope="scope">
                    {{ scope.row.rank ? scope.row.rank : '--' }}
                </template>
            </el-table-column>
            <el-table-column
                prop="award_money"
                label="获得奖金"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="left"
                sortable="custom"
                :width="120"
            >
                <template slot-scope="scope">
                    {{ scope.row.award_money ? scope.row.award_money : 0 }}
                </template>
            </el-table-column>
            <el-table-column
                prop="award_status"
                label="派发状态"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="left"
            >
                <template slot-scope="scope">
                    <div class="__flex_row">
                        <span
                            :style="{background: scope.row.award_status == 1 ? 'rgba(76, 217, 100, 1)' : 'rgba(255, 167, 45, 1)'}"
                            class="__flex_row_span"
                        />
                        <span>{{ scope.row.award_status === 1 ? '已发放' : '未发放' }}</span>
                    </div>
                </template>
            </el-table-column>
            <!--<el-table-column
                prop="award_status"
                label="派发类型"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="left"
            >
                <template slot-scope="scope">
                    {{ scope.row.award_type === 1 ? '系统派发' : '手动派发' }}
                </template>
            </el-table-column>-->
            <el-table-column
                label="活动资格"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="left"
            >
                <template slot-scope="scope">
                    <div class="status-wrapper">
                        <div
                            :class="['status-dot', scope.row.cancel_status === 1 ? 'success' : 'danger']"/>
                        {{ scope.row.cancel_status === 1 ? '未取消' : '已淘汰' }}
                    </div>
                </template>
            </el-table-column>
            <el-table-column
                label="操作"
            >
                <template slot-scope="scope">
                    <el-button
                        type="text"
                        class="curse-add"
                        :disabled="scope.row.cancel_status === -2"
                        @click="handleCancel(scope.row)"
                    >
                        淘汰
                    </el-button>
                    <el-button
                        type="text"
                        class="curse-add"
                        :disabled="scope.row.can_award === 0"
                        @click="handleSendAward(scope.row)"
                    >
                        发放奖励
                    </el-button>
                </template>
            </el-table-column>
        </el-table>
        <template slot="pagination">
            <div style="margin-left: 22px">
                <el-checkbox
                    label="当页全选"
                    v-model="checkAllStatus"
                    :indeterminate="checkIndeterminate"
                    :disabled="tableData.length === 0"
                    @change="handleSelectedAll"
                />
                <el-button
                    type="text"
                    size="medium"
                    style="margin-left: 12px"
                    :disabled="multipleSelection.length === 0"
                    @click="handleSetAwardBatch"
                >
                    批量发放奖励
                </el-button>
            </div>
            <el-pagination
                background
                layout="total, prev, pager, next"
                :current-page.sync="page.now_page"
                :total="page.total_count"
                :page-size="page.page_size"
                @current-change="getTableData"
            />
        </template>
    </PageLayout>
</template>

<script>
import { back } from '@/mixin/back';
export default {
    mixins: [back],
    components: { },
    data () {
        return {
            formData: {
                keyword: '',
                active_number: '',
                award_type: 99,
                award_status: 99,
                school_keyword: '',
                cancel_status: 99,
                index: -1
            },
            activeList: [],
            typeList: [{value: 99, label: '全部'},{value: 1, label: '系统派发'},{value: 2, label: '手动派发'}],
            statusList: [{value: 99, label: '全部'},{value: 1, label: '正常'},{value: -2, label: '淘汰'}],
            filterData: {
                sort_field: '',
                sort_type: 0
            },
            page: {
                now_page: 1,
                total_count: 0,
                page_size: 0
            },
            activity_info: {
                title: '',
                index: '',
                activity_start_time: '',
                activity_end_time: '',
                rank_end_time: ''
            },
            tableData: [],
            school_tree: [],
            authResult: {},
            checkAllStatus: false,
            checkIndeterminate: false,
            multipleSelection: []
        };
    },
    filters: {},
    computed: {},
    watch: {},
    mounted () {
        this.$store.commit('SET_HEADER', [{ title: "APP后台"},{title: '校园精英pk赛'},{title: '统计列表'},{title: '奖励派发', path: '/student/elite-pk-statistics?active=school-award'},{title: '查看详情'}]);
        this.authResult = this.$route.meta.authResult;
        this.formData.school_keyword = this.$route.query.school_name ? this.$route.query.school_name : '';
        this.formData.index = this.$route.query.index ? this.$route.query.index : -1;
        this.getActiveNumber();
        this.getSchoolTree();
    },
    methods: {

        /**
         * @description 获取省/市/学校三级联动
         */
        getSchoolTree () {
            this.$post('/student/local_info%5Cschool_tree',{},resp => {
                if (resp.code === 1) {
                    this.school_tree = resp.data.tree;
                }
            });
        },

        /**
         * @description 获取表格数据
         */
        getTableData (page) {
            this.$post('/student/task_stuagent_extra_pk_activity%5Cactivity_statistic%5Cget_stuagent_award_list', {
                page_id: page || this.page.now_page,
                sort_field: this.filterData.sort_field,
                sort_type: this.filterData.sort_type,
                keyword: this.formData.keyword,
                cancel_status: this.formData.cancel_status,
                school_keyword: this.formData.school_keyword,
                index: this.formData.index
            }, resp => {
                if (resp.code === 1) {
                    this.tableData = resp.data.list;
                    this.page.now_page = resp.data.page.now_page;
                    this.page.total_count = resp.data.page.total_count;
                    this.page.page_size = resp.data.page.page_size;

                }
            });
        },

        /**
         * @description 发放奖励
         */
        handleSendAward (row) {
            this.$zdConfirm('是否确定发放该笔奖金?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.$post('/student/task_stuagent_extra_pk_activity%5Cactivity_statistic%5Cset_award',{
                    apply_no: row.apply_no
                },resp=>{
                    if (resp.code == 1) {
                        this.getTableData();
                        this.$notify({
                            title: '提示',
                            message: '发放成功',
                            type: 'success',
                            duration: 2000
                        });
                    } else if (resp.code == 0) {
                        this.$notify({
                            title: '提示',
                            message: resp.msg,
                            type: 'warning',
                            duration: 2000
                        });
                    }
                });
            });
        },

        /**
         * @description 获取期数
         */
        getActiveNumber () {
            this.$post('/student/task_stuagent_extra_pk_activity%5Cactivity_config%5Cget_list_for_select',{},resp => {
                if (resp.code === 1) {
                    const activeList = resp.data.list.reduce((cur,next) => {
                        cur.push({
                            index: next.index,
                            title: next.index
                        });
                        return cur;
                    }, []);

                    this.activeList = [{index: -1, title: '全部'}].concat(activeList);
                    this.$set(this.formData, 'active_number', !!this.activeList ? (!!this.activeList.length ? this.activeList[0]['index'] : -1) : -1);
                    this.getTableData();
                } else {
                    this.getTableData();
                }
            });
        },

        /**
         * @description 数据排序
         */
        handleSortChanged(data) {
            let sortTypeMap = {
                ascending: 1,
                descending: 0
            };

            let sortFieldMap = {
                team_order_money: 'team_order_money',
                team_real_order_count: 'team_real_order_count',
                rank: 'rank',
                award_money: 'award_money'
            };

            if (data.order) {
                this.filterData.sort_field = sortFieldMap[data.prop];
                this.filterData.sort_type = sortTypeMap[data.order];
            } else {
                this.filterData.sort_field = '';
                this.filterData.sort_type = '';
            }

            this.getTableData();
        },

        /**
         * @description 过滤函数
         */
        emptyFormatter(row, column, val) {
            if (!val) return '--';
            return val;
        },

        /**
         * 取消资格
         * @param row
         */
        async handleCancel(row) {
            let cancel_reason = await this.$zdPrompt({
                title: '淘汰',
                message: '淘汰原因',
                placeholder: '请输入淘汰原因…',
                maxlength: ''
            });

            if(cancel_reason) {
                let params = {
                    apply_no: row.apply_no,
                    status: -2,
                    cancel_reason
                };

                this.$post('/student/task_stuagent_extra_pk_activity%5Cstudent_apply%5Cset_status', params, resp => {
                    if (resp.code === 1) {
                        this.$notify({
                            title: '提示',
                            message: '操作成功',
                            type: 'success',
                            duration: 2000
                        });
                        this.getTableData();
                    } else {
                        this.$notify({
                            title: '提示',
                            message: resp.msg,
                            type: 'warning',
                            duration: 2000
                        });
                    }
                });
            }
            // this.$zdConfirm('是否确认淘汰该用户', '温馨提示', {
            //     confirmButtonText: '确认',
            //     cancelButtonText: '取消',
            //     type: 'warning',
            // }).then(() => {
            //
            // }).catch(() => {});
        },

        /**
         * 批量发放奖励
         */
        handleSetAwardBatch() {
            this.$zdConfirm('是否要批量发放奖励?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.$post('/student/task_stuagent_extra_pk_activity%5Cactivity_statistic%5Cset_award_batch',{
                    apply_nos: this.multipleSelection.map(item => {
                        return item.apply_no;
                    }).join()
                },resp=>{
                    if (resp.code === 1) {
                        this.getTableData();
                        this.$notify({
                            title: '提示',
                            message: '发放成功',
                            type: 'success',
                            duration: 2000
                        });
                    } else {
                        this.$notify({
                            title: '提示',
                            message: resp.msg,
                            type: 'warning',
                            duration: 2000
                        });
                    }
                });
            });
        },

        /**
         * 路由跳转
         * @param routeName
         * @param query
         */
        handleRoute(routeName, query) {
            this.$router.push({
                name: routeName,
                query
            });
        },

        /**
         * 点击全选
         * @param val
         */
        handleSelectedAll(val) {
            if(val) {
                this.$refs.zsdxEliteSend.toggleAllSelection();
            } else {
                this.$refs.zsdxEliteSend.clearSelection();
            }
            this.checkIndeterminate = false;
        },

        /**
         * 选中
         * @param selection
         */
        handleSelectedChange(selection) {
            let checkedCount = selection.length;

            this.multipleSelection = selection;
            this.checkAllStatus = checkedCount === this.tableData.length;
            this.checkIndeterminate = checkedCount > 0 && checkedCount < this.tableData.length;
        },
    }
};
</script>

<style lang="less" scoped>
    .zsdxEliteSend {
        background-color: #fff;
        padding: 24px 32px 5px 32px;
        display: flex;
        flex-direction: column;
        position: relative;
        width: 100%;
        height: 100%;
        .header-nav {
            position: relative;
            display: flex;
            flex-wrap: wrap;
            align-items: center;
            justify-content: space-between;
            .header-left, .header-right {
                display: flex;
                align-items: center;
                flex-wrap: wrap;
            }
            .header-new-box {
                display: flex;
                margin-right: 15px;
                align-items: center;
                color: #606266;
                margin-bottom: 22px;
                white-space: nowrap;
                &-title {
                    margin-right: 2px;
                    white-space: nowrap;
                }
            }
        }
        /* 表单 */
        .scope-price {
            width: 50px;
            height: 50px;
        }
        /* 字体高亮 */
        .cursor-font {
            color: rgb(64, 158, 255);
            cursor: pointer;
        }
        /* 分页 */
        .pagination-wrapper {
            padding-top: 10px;
            padding-bottom: 10px;
            display: flex;
            align-items: center;
            justify-content: space-between;
            .el-pagination {
                float: right;
                padding: 0;
                background-color: transparent;
            }
        }
    }
    /* 排序 */
    .sort-wrap {
        display: flex;
        align-items: center;
        justify-content: center;
        cursor: pointer;
        .sort-spliter {
            width: 1px;
            height: 12px;
            background: rgba(235, 235, 235, 1);
            margin: 0 10px;
            }
            .btn-sort {
            font-size: 14px;
            line-height: 1;
            color: #ccc;
            &:hover {
                color: #20a0ff;
            }
        }
    }
    .el-button-left {
        margin-left: 40px;
    }
    .el-button-right {
        margin-right: 10px;
    }
    .__flex_wrap {
        display: flex;
        flex-direction: column;
        justify-content: flex-start;
        align-items: flex-start;
        .__flex_wrap_title {
            color: rgba(51, 51, 51, 1);
            font-size: 14px;
            font-weight: 500;
        }
    }
    .__flex_row {
        display: flex;
        align-items: center;
        .__flex_row_span {
            display: inline-block;
            width: 6px;
            height: 6px;
            border-radius: 50%;
            margin-right: 4px;
        }
    }
    // 表格
    .data-table {
        ::-webkit-scrollbar {
            width: 6px;
            height: 6px;
        }
    }
</style>
